package org.feisoft.jta;

import org.feisoft.transaction.Transaction;
import org.feisoft.transaction.TransactionRepository;
import org.feisoft.transaction.xa.TransactionId;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TransactionRepositoryImpl implements TransactionRepository
{
    private final Map<TransactionId, Transaction> xidToTxMap = new ConcurrentHashMap<TransactionId, Transaction>();
    private final Map<TransactionId, Transaction> xidToErrTxMap = new ConcurrentHashMap<TransactionId, Transaction>();

    public void putTransaction(TransactionId globalXid, Transaction transaction)
    {
        this.xidToTxMap.put(globalXid, transaction);
    }

    public Transaction getTransaction(TransactionId globalXid)
    {
        return this.xidToTxMap.get(globalXid);
    }

    public Transaction removeTransaction(TransactionId globalXid)
    {
        return this.xidToTxMap.remove(globalXid);
    }

    public void putErrorTransaction(TransactionId globalXid, Transaction transaction)
    {
        this.xidToErrTxMap.put(globalXid, transaction);
    }

    public Transaction getErrorTransaction(TransactionId globalXid)
    {
        return this.xidToErrTxMap.get(globalXid);
    }

    public Transaction removeErrorTransaction(TransactionId globalXid)
    {
        return this.xidToErrTxMap.remove(globalXid);
    }

    public List<Transaction> getErrorTransactionList()
    {
        return new ArrayList<Transaction>(this.xidToErrTxMap.values());
    }

    public List<Transaction> getActiveTransactionList()
    {
        return new ArrayList<Transaction>(this.xidToTxMap.values());
    }

}
